// myself/daily/HealthRecord.js
import api from "../../utils/api.js";
import wechat from '../../utils/wechat.js';
import network from '../../utils/network.js';
import formatDate from "../../utils/format_date.js";
import notification from '../../utils/notification_center.js';
const app = getApp();
let nowPage = 0;
let maxPage = 1;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    list: [],
    page: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    notification.addNotification("HealthRecord", this.refreshNotification, this);
    this.toast = this.selectComponent("#toast");
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    notification.removeNotification("HealthRecord", this);
    nowPage = 0;
    maxPage = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.request();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.request();
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */
  request: function() {
    if (nowPage == maxPage) {
      this.toast.showToast("暂无更多");
    } else {
      network.postJson(this, api.fangangList, {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,
        page: this.data.page
      }).then(res => {
        this.setData({
          [`list[${nowPage}]`]: res.data.data.rows,
          page: res.data.data.nextpage
        })

        nowPage = res.data.data.page;
        maxPage = res.data.data.maxpage;
      });
    }
  },

  refreshNotification: function() {
    this.onPullDownRefresh();
  },

  goHealthDaily() {
    wx.navigateTo({
      url: '/myself/daily/HealthDaily'
    })
  }
})